package com.sky.interceptor;

import cn.dev33.satoken.stp.StpUtil;
import com.sky.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * jwt令牌校验的拦截器
 */
@Component
@Slf4j
public class TokenUserInterceptor implements HandlerInterceptor {

        @Override
        public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws
        Exception {
            log.info("token拦截器工作...");
            String token = request.getHeader("authentication");
            log.info("token:{}", token);
            Object id = StpUtil.getLoginIdByToken(token);
            log.info("id:{}", id);
            Long userId = Long.parseLong(id + "");
            BaseContext.setCurrentId(userId);
            return HandlerInterceptor.super.preHandle(request, response, handler);
        }

    }

